package com.carson.undergo.ui.activity.home

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import com.carson.undergo.R
import com.carson.undergo.base.ApiService
import com.carson.undergo.databinding.UiChooseActivityBinding
import com.model.common.base.activity.BaseActivity
import com.model.common.utils.always.StringUtils
import com.model.common.utils.always.ToastUtils
import com.model.common.utils.http.RetrofitHelper
import com.model.common.utils.http.back.HttpError
import com.model.common.utils.http.back.NetWorkCallback
import com.model.common.utils.model.city.AreaBean
import com.model.common.utils.model.city.CityBean
import com.model.common.utils.model.city.ProvinceBean
import com.model.common.utils.view.MyCityPicker
import retrofit2.Call

/**
 * Create by Carson on 2022/2/22.
 * 自定义三级联动列表选择省市区
 */
class ChooseCityUI : BaseActivity<UiChooseActivityBinding>() {

    //所有省
    private var mProvinceData = ArrayList<ProvinceBean>()

    //key - 省 value - 市
    private var mCityDataMap = HashMap<String, ArrayList<CityBean>>()

    //key - 市 values - 区
    private var mDistrictDataMap = HashMap<String, ArrayList<AreaBean>>()

    //选择的省市区信息
    private var provinceId = ""
    private var provinceName = ""
    private var cityId = ""
    private var cityName = ""
    private var areaId = ""
    private var areaName = ""

    override fun getViewBinding(): UiChooseActivityBinding =
        UiChooseActivityBinding.inflate(layoutInflater)

    override fun initViewsAndEvents() {

    }

    override fun addListener() {
        binding.chooseCity.setOnClickListener(this)
        binding.tvSure.setOnClickListener(this)
    }

    override fun onWidgetClick(view: View?) {
        when (view?.id) {
            //选择省市区
            R.id.choose_city -> requestCity()
            //确定
            R.id.tv_sure -> {
                if (StringUtils.isEmpty(provinceName)) {
                    ToastUtils.showMessage(mContext, "请先选择省市区")
                    return
                }
                ToastUtils.showMessage(mContext, binding.chooseCity.text.toString().trim())
                finish()
            }
        }
    }

    /**
     * 获取省市区信息
     */
    private fun requestCity() {
        RetrofitHelper.create(ApiService::class.java)
            .requestCity()
            .enqueue(this, object : NetWorkCallback<ArrayList<ProvinceBean>?>(mContext) {
                override fun onError(call: Call<ArrayList<ProvinceBean>?>?, error: HttpError?) {
                    ToastUtils.showMessage(mContext, error?.errorMessage)
                }

                override fun onSuccess(
                    call: Call<ArrayList<ProvinceBean>?>?,
                    response: ArrayList<ProvinceBean>?
                ) {
                    if (response != null && response.size > 0) {
                        mProvinceData = response
                        mCityDataMap = HashMap()
                        mDistrictDataMap = HashMap()
                        for (param in response) {
                            if (param.children != null && param.children.size > 0) {
                                mCityDataMap[param.value] = param.children as ArrayList<CityBean>
                                for (item in param.children) {
                                    if (item.children != null && item.children.size > 0) {
                                        mDistrictDataMap[item.value] =
                                            item.children as ArrayList<AreaBean>
                                    }
                                }
                            }
                        }
                        selectAddress()//调用CityPicker选取区域
                    } else {
                        ToastUtils.showMessage(mContext, "城市信息为空")
                    }
                }
            })
    }

    @SuppressLint("SetTextI18n")
    private fun selectAddress() {
        val cityPicker = MyCityPicker.Builder(this)
            .textSize(14)
            .title("地址选择")
            .titleBackgroundColor("#FFFFFF")
            .confirTextColor("#33CCCC")
            .cancelTextColor("#999999")
            .textColor(Color.parseColor("#000000"))
            .provinceCyclic(false)
            .cityCyclic(false)
            .districtCyclic(false)
            .visibleItemsCount(7)
            .itemPadding(10)
            .onlyShowProvinceAndCity(false)
            .build()
        cityPicker.provinceData = mProvinceData
        cityPicker.cityDataMap = mCityDataMap as Map<String, MutableList<CityBean>>?
        cityPicker.districtDataMap = mDistrictDataMap as Map<String, MutableList<AreaBean>>?
        cityPicker.show()
        //监听方法，获取选择结果
        cityPicker.setOnCityItemClickListener { citySelected -> //获取省市区三级的城市ID
            provinceId = mProvinceData[citySelected[0]].value
            provinceName = mProvinceData[citySelected[0]].label
            cityId = mProvinceData[citySelected[0]].children[citySelected[1]].value
            cityName = mProvinceData[citySelected[0]].children[citySelected[1]].label
            areaId =
                mProvinceData[citySelected[0]].children[citySelected[1]].children[citySelected[2]].value
            areaName =
                mProvinceData[citySelected[0]].children[citySelected[1]].children[citySelected[2]].label
            binding.chooseCity.text = "$provinceName-$cityName-$areaName"
        }
    }
}